########################
#IACtHR Matching 
#Set Working Directory Below
#Use "IACtHRMatchingData.dta" (Stata v. 13)
#######################

#check if packages are installed, and if not, install them
packages <- c("foreign", "readstata13", "MatchIt")
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
  install.packages(setdiff(packages, rownames(installed.packages())), repos="http://cran.rstudio.com/")  
}
install.packages("Zelig")
library(Zelig)

#attach packages
lapply(packages, library, character.only = T)

#default the stringsAsFactors argument to FALSE for all functions with that argument
options(stringsAsFactors=FALSE)

#turn off scientific notation
options(scipen=999)

#set seed
set.seed(33603)

#set working directory
setwd()

#load data
d <- read.dta13("IACtHRMatchingData.dta")


#keep the variables we want for analysis
data <- d[, c("ccode", "year", "repressionlatent", "numcasejud_1", "iacthr_numcase_1", "iacase_bin_1", "lji_1", "lfdi_1", "speech_1", "cs_1", "nhri_1", "polconiii_1", "xrcomp_1", "cwar_1", "v2xel_frefair_1", "popmillionslog_1", "gdpcaplog_1", "regionalcountbin_1", "id")]

#xtreg repressionlatent numcasejud_1 iacthr_numcase_1 lji_1 lfdi_1 speech_1 cs_1 nhri_1 xrcomp_1 polconiii_1 v2xel_frefair_1 popmillionslog_1 gdpcaplog_1 cwar_1 regionalcountbin_1 if ccode != 52 & ccode != 53 & ccode != 115, re vce(cluster id) level(90)


#listwise delete
data1 <- na.omit(data)

#attach data
attach(data1)

#########################################################
#Matching - Main Model

#Nearest Neighbor (one to one)
nearest.out1<-matchit(iacase_bin_1 ~ lji_1 + lfdi_1 + speech_1 + cs_1 + nhri_1 + polconiii_1 + xrcomp_1 + cwar_1 + v2xel_frefair_1 + popmillionslog_1 + gdpcaplog_1 + regionalcountbin_1, data = data1, method ="nearest", distance = "logit", discard = "both", reestimate = TRUE)

##Table 5##
nearest.out1
summary(nearest.out1)

#Summary of balance for all data:
#  Means Treated Means Control SD Control Mean Diff eQQ Med eQQ Mean eQQ Max
#distance                  0.3110        0.1275     0.1413    0.1835  0.2132   0.1823  0.2531
#lji_1                     0.4865        0.5580     0.1996   -0.0715  0.0311   0.0781  0.3610
#lfdi_1                    0.8271        0.6696     1.1870    0.1575  0.1310   0.2770  3.6368
#speech_1                  1.1739        1.4497     0.5390   -0.2758  0.0000   0.2609  1.0000
#cs_1                      0.7319        0.7544     0.1453   -0.0225  0.0299   0.0369  0.2415
#nhri_1                    0.8406        0.5820     0.4939    0.2586  0.0000   0.2609  1.0000
#polconiii_1               0.3167        0.3835     0.1425   -0.0667  0.0600   0.0682  0.1333
#xrcomp_1                  2.8406        2.7725     0.5058    0.0681  0.0000   0.0870  1.0000
#cwar_1                    0.2464        0.1534     0.3609    0.0929  0.0000   0.0870  1.0000
#v2xel_frefair_1           0.7107        0.7328     0.2139   -0.0221  0.0611   0.0660  0.3344
#popmillionslog_1          2.7463        2.3360     1.2312    0.4103  0.5856   0.5251  0.9751
#gdpcaplog_1               8.0952        7.8814     0.8613    0.2137  0.1468   0.2447  1.0190
#regionalcountbin_1        0.9710        0.5714     0.4955    0.3996  0.0000   0.4058  1.0000


#Summary of balance for matched data:
#  Means Treated Means Control SD Control Mean Diff eQQ Med eQQ Mean eQQ Max
#distance                  0.3110        0.3066     0.1348    0.0044  0.0015   0.0057  0.0612
#lji_1                     0.4865        0.4917     0.1570   -0.0052  0.0154   0.0236  0.2157
#lfdi_1                    0.8271        0.8038     1.2051    0.0233  0.1356   0.2407  1.2259
#speech_1                  1.1739        1.2029     0.5578   -0.0290  0.0000   0.0580  1.0000
#cs_1                      0.7319        0.7471     0.1368   -0.0152  0.0228   0.0304  0.1796
#nhri_1                    0.8406        0.8841     0.3225   -0.0435  0.0000   0.0435  1.0000
#polconiii_1               0.3167        0.3271     0.1400   -0.0103  0.0194   0.0204  0.0867
#xrcomp_1                  2.8406        2.8406     0.5320    0.0000  0.0000   0.0870  1.0000
#cwar_1                    0.2464        0.2029     0.4051    0.0435  0.0000   0.0435  1.0000
#v2xel_frefair_1           0.7107        0.7325     0.1823   -0.0218  0.0284   0.0415  0.3344
#popmillionslog_1          2.7463        2.5685     1.2572    0.1777  0.2790   0.2590  0.8713
#gdpcaplog_1               8.0952        8.1247     0.9121   -0.0296  0.1527   0.2121  1.0960
#regionalcountbin_1        0.9710        0.9710     0.1690    0.0000  0.0000   0.0000  0.0000

#Percent Balance Improvement:
#  Mean Diff. eQQ Med eQQ Mean  eQQ Max
#distance              97.5919 99.2968  96.8850  75.8235
#lji_1                 92.7058 50.4823  69.7712  40.2493
#lfdi_1                85.1906 -3.5019  13.1153  66.2926
#speech_1              89.4912  0.0000  77.7778   0.0000
#cs_1                  32.4624 23.7135  17.5841  25.6363
#nhri_1                83.1851  0.0000  83.3333   0.0000
#polconiii_1           84.5176 67.7411  70.1429  35.0000
#xrcomp_1             100.0000  0.0000   0.0000   0.0000
#cwar_1                53.2178  0.0000  50.0000   0.0000
#v2xel_frefair_1        1.4267 53.5741  37.0896   0.0000
#popmillionslog_1      56.6787 52.3647  50.6803  10.6410
#gdpcaplog_1           86.1627 -4.0106  13.3155  -7.5564
#regionalcountbin_1   100.0000  0.0000 100.0000 100.0000

#Sample sizes:
#  Control Treated
#All           378      69
#Matched        69      69
#Unmatched     293       0
#Discarded      16       0

#create data frame
nearest.data1<-match.data(nearest.out1)

#Regression
reg<-zelig(repressionlatent ~ numcasejud_1 + iacthr_numcase_1 + lji_1 + lfdi_1 + speech_1 + cs_1 + nhri_1 + polconiii_1 + xrcomp_1 + cwar_1 + v2xel_frefair_1 + popmillionslog_1 + gdpcaplog_1 + regionalcountbin_1, data = nearest.data1, model= "normal")

#xtreg repressionlatent numcasejud_1 iacthr_numcase_1 lji_1 lfdi_1 speech_1 cs_1 nhri_1 xrcomp_1 polconiii_1 v2xel_frefair_1 popmillionslog_1 gdpcaplog_1 cwar_1 regionalcountbin_1 if ccode != 52 & ccode != 53 & ccode != 115, re vce(cluster id) level(90)

##Table 6##
summary(reg)

#Deviance Residuals: 
#  Min        1Q    Median        3Q       Max  
#-1.31774  -0.30500  -0.03019   0.35571   1.20358  

#Coefficients:
#  Estimate Std. Error t value             Pr(>|t|)
#numcasejud_1        0.66506    0.42260   1.574              0.11812
#iacthr_numcase_1   -0.34679    0.21251  -1.632              0.10527
#lji_1              -0.30616    0.54412  -0.563              0.57468


#Null deviance: 127.416  on 137  degrees of freedom
#Residual deviance:  31.705  on 123  degrees of freedom
#AIC: 220.66







